package com.dayu.recovery.domain;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.dayu.basic.domain.BasicTestValues;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.recovery.domain.constant.GoodsConstant;
import com.dayu.store.domain.StoreCollect;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import com.dayu.common.annotation.Excel;
import com.dayu.common.core.domain.BaseEntity;
import org.springframework.util.ObjectUtils;

/**
 * 回收商品管理对象 recovery_goods
 *
 * @author HK
 * @date 2021-04-16
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class RecoveryGoods extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 商品编码 */
    @Excel(name = "商品编码")
    private String goodsId;

    /** 收货编号 */
    @Excel(name = "收货编号")
    private Long collectId;

    @Excel(name = "供应商")
    private String supplierName;

    /** 商品型号 */
    @Excel(name = "商品型号")
    private String goodsName;

    /** 商品串号 */
    @Excel(name = "商品串号")
    private String goodsSn;

    /** 商品型号编号 */
    private Long shareGoodsId;

    /** 规格组 */
    private String specsIds;

    /** 打印内容 */
    @Excel(name = "规格")
    private String printing;
    /** 验机报告 */
    private String testValues;
    @Excel(name = "验机报告")
    private String testInfo;

    /** 回收价格 */
    @Excel(name = "回收价格")
    private BigDecimal costPrice;

    @Excel(name = "调价记录", width = 60, height = 40)
    private String priceList;

    /** 库存编号 */
    private Long stockId;

    @Excel(name = "仓库")
    private String stockName;

    /** 库存排序 */
    @Excel(name = "库存排序")
    private Long stockNum;

    /** 条码路径 */
    private String barCode;

    /** 确认状态 */
    @Excel(name = "确认状态", readConverterExp = "1=无,2=待确认")
    private Integer confirmStatus;

    /** 商品状态 1=正常，2=借出中，3=丢失中，4=作废，5=出售 */
    @Excel(name = "商品状态", readConverterExp = "1=正常,2=借出中,3=丢失中,4=作废, 5=出售")
    private Integer goodsStatus;

    @Excel(name = "借出人")
    private String loanName;

    @Excel(name = "创建时间", dateFormat = "yyyy-MM-dd  HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** 门店回收*/
    private Long storeId;

    //报价师
    @Excel(name = "报价师")
    private String offerName;

    //验机人
    @Excel(name = "验机人")
    private String testName;

    //验机时间
    @Excel(name = "验机时间")
    private String testTime;


    @Excel(name = "打款", readConverterExp = "1=已打款")
    private Integer hasPay;

    @Excel(name = "备注")
    private String remark;

    /** 供应商id*/
    private Long sourceId;

    /*模糊查询id*/
    private String likeGoodsId;

    /** 修改日志id */
    private Long recordId;

    //成色
    private String fineness;

    /*预售价格列表*/
    private List<SalePriceLog> salePriceList;

    private StoreCollect storeCollect;

    private Integer updateStatus;

    //步骤 1=待验机 2=已验机
    private Integer goodsStep;


    public AjaxResult goodsVerify() {
        if(ObjectUtils.isEmpty(this.shareGoodsId))
            return AjaxResult.error("请确认是否完成验机！");
        if(ObjectUtils.isEmpty(this.costPrice))
            return AjaxResult.error("请确认是否完成报价！");
        if(this.getGoodsStatus() == GoodsConstant.GOODS_STATUS_LOAN) {
            return AjaxResult.error("商品借出中，不可操作！");
        }
        if(this.getGoodsStatus() == GoodsConstant.GOODS_STATUS_LOSS) {
            return AjaxResult.error("商品丢失中，不可操作！");
        }
        if(this.getGoodsStatus() == GoodsConstant.GOODS_STATUS_DEL) {
            return AjaxResult.error("商品已作废，请联系管理员！");
        }
        if(this.getGoodsStatus() == GoodsConstant.GOODS_STATUS_SALE) {
            return AjaxResult.error("商品已作废，请联系管理员！");
        }
        if(this.getConfirmStatus() == GoodsConstant.CONFIRM_STATUS_NO) {
            return AjaxResult.error("商品待确认，请完成前置操作（如移库）！");
        }
        return null;
    }

    public void builderByScattered(RecoveryScattered scattered) {
        this.goodsName = scattered.getGoodsName();
        this.goodsSn = scattered.getGoodsSn();
        this.shareGoodsId = scattered.getShareGoodsId();
        this.specsIds = scattered.getSpecsIds();
        this.printing = scattered.getPrinting();
        this.testValues = scattered.getTestValues();
        this.costPrice = scattered.getPrice();
        this.fineness = scattered.getFineness();
        this.setRemark(scattered.getRemark());
    }

    /**
     * 验机报告转换
     * @param list 验机选项
     */
    public void builderTestValuesByList(List<BasicTestValues> list) {
        this.testValues = builderTestValuesString(list);
    }

    public String builderTestValuesString(List<BasicTestValues> list) {
        if(StringUtils.isEmpty(this.testValues)) {
            return null;
        }
        String[] arr = this.testValues.split(",");
        StringBuilder res = new StringBuilder();
        for(int i = 0; i < arr.length; i++) {
            Long selId = Long.parseLong(arr[i]);
            for(BasicTestValues values : list) {
                if(values.getValuesId().equals(selId)) {
                    res.append("[").append(values.getValuesName()).append("]-");
                    break;
                }
            }
        }
        return res.substring(0, res.length() - 1);
    }

    public List<RecoveryModify> builderModifyList(List<RecoveryModify> list) {
        SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd");
        StringBuilder arr = new StringBuilder();
        List<RecoveryModify> nowList = new ArrayList<>();
        for (RecoveryModify m : list) {
            //相同
            if(Objects.equals(m.getOrderNum(), this.goodsId)) {
                if(m.getBaseType() == 2) {
                    continue;
                }
                String a = df.format(m.getCreateTime()) + " 金额(前): " + m.getPrePrice() + "元 ";
                if (m.getStatus() == 1) {
                    a += "上调 ";
                } else {
                    a += "下调 ";
                }
                a += m.getModPrice() + "元"+ (ObjectUtils.isEmpty(m.getRemark()) ? "": "(" + m.getRemark() + ")")
                        + " 金额(后): " + m.getSufPrice() + "元";
                arr.append(a).append("\r\n");
            }else {
                nowList.add(m);
            }
        }
        this.priceList = arr.toString();
        return nowList;
    }
}
